<template>
  <div class="login-container" :loading="loading" :style="{ backgroundImage: 'url(' + bg + ')' }">
    <el-form class="login-form" ref="loginFormRef" :model="userDto" :rules="loginRules">
      <div class="title-container">
        <h3 class="title"> 厦门大学附属翔安医院欢迎您！！！</h3>
        <lang-select class="lang-select"></lang-select>
      </div>
      <!-- username -->
      <el-form-item prop="userName">
        <span class="svg-container">
          <span class="svg-container">
            <svg-icon icon="user"></svg-icon>
          </span>
        </span>
        <el-input class="login-default-input"  placeholder="userName" name="userName" type="text" v-model="userDto.userName"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
         <span class="svg-container">
            <svg-icon icon="password"></svg-icon>
          </span>
        </span>
        <el-input class="login-default-input" placeholder="password" name="password" :type="passwordType"  v-model="userDto.password"></el-input>
        <span class="show-pwd">
          <span class="svg-container" @click="onChangePwdType">
            <svg-icon :icon="passwordType === 'password'? 'eye-close':'eye'"></svg-icon>
          </span>
        </span>
      </el-form-item>
      <el-form-item prop="password">
        <el-input class="login-default-input" placeholder="验证码" name="code"  v-model="userDto.code"></el-input>
        <div class="show-code" @click="changePic">
           <el-image :src="src" />
        </div>
      </el-form-item>
      <el-button type="primary" style="width:48%" @click="handerLogin">登录</el-button>
      <el-button type="primary" style="width:48%" @click="handerRegister">注册</el-button>
      <!-- <div class="tips" v-html="$t('msg.login.desc')"></div> -->
    </el-form>
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { useStore } from 'vuex'
import { validatePassword } from './rules'
import LangSelect from '@/components/LangSelect/index'
import { useI18n } from 'vue-i18n'
import router from '@/router'
import { getValidCode } from '@/api/common'
import { ElMessage } from 'element-plus'

const bg = require('@/assets/bg.jpg')

// 登录验证码
const src = ref('')

// 处理登录
const loading = ref(false)

// 生成验证码
const generateVaildCode = async () => {
  loading.value = true
  getValidCode('LOGIN').then(res => {
    src.value = res.result
    loading.value = false
  }).catch(err => {
    loading.value = false
    ElMessage.error(err.message)
  })
}

onActivated(generateVaildCode())

const changePic = () => {
  generateVaildCode()
}

// 数据源
const userDto = ref({
  userName: '',
  password: ''
})
// 验证规则
const i18n = useI18n()
const loginRules = ref({
  userName: [
    {
      required: true,
      trigger: 'blur',
      message: '用户名不能为空'
    }
  ],
  password: [
    {
      required: true,
      trigger: 'blur',
      validator: validatePassword()
    }
  ]
})
// 处理密码框文本显示
const passwordType = ref('password')
// template 中绑定的方法，直接声明即可
const onChangePwdType = () => {
  // 当passwordType的值为password的时候，改为text
  // 使用 ref 声明的数据，在script中使用时，需要加value来获取具体的值,但是再template中使用的时候，不需要加value
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
}

const store = useStore()
const loginFormRef = ref(null)
const handerLogin = () => {
  // 1.进行表单校验
  loginFormRef.value.validate(valid => {
    if (!valid) return
    // 2.触发登录动作
    loading.value = true
    store.dispatch('user/login', userDto.value).then(() => {
      loading.value = false
      // 3.进行登录处理
    }).catch(err => {
      loading.value = false
      ElMessage.error(err.message)
    })
  })
}
// 注册
const handerRegister = () => {
  router.push('/register')
}
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray:#889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-size: cover; /* 或者其他适合的值 */
  background-repeat: no-repeat;
  background-position: center;
  .show-code {
    width: 120px;
    height: 40px;
    position: absolute;
    right: 2px;
  }
  .login-form {
    position: relative;
    width: 720px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    // background-color: aquamarine;
    ::v-deep(.el-form-item) {
      border: 1px solid rgba(255,255,255,0.1);
      background-color: rgba(0,0,0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    ::v-deep(.el-input) {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        background: transparent;
        border: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        caret-color: $cursor;
      }
    }
    .tips {
      font-size: 16px;
    }
    ::v-deep(.login-default-input) {
      & input {
        box-shadow: none;
        height: 100%;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
   }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  ::v-deep(.lang-select) {
    position: absolute;
    top: 4px;
    right: 0;
    background-color: #fff;
    font-size: 22px;
    padding: 4px;
    border-radius: 4px;
    cursor: pointer;
  }
}
</style>
